package com.yinhai.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.lz.util.Utils;
import com.yinhai.activity.R;
import com.yinhai.activity.SystemSetupMainActivity;
import com.yinhai.service.MsgServiceImpl;

/**
 * 系统设置分组列表
 * 
 * @author Administrator
 *
 */
public class SystemSetupMainAdpter extends BaseAdapter {
    private LayoutInflater mInflater;
    
    private List<String> systemNameList;
    
    private AQuery aq = null;
    
    SystemSetupMainActivity context = null;
    
    public SystemSetupMainAdpter(SystemSetupMainActivity context) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.systemNameList = new ArrayList<String>();
    }
    
    public void assignment(List<String> systemNameList) {
        this.systemNameList = systemNameList;
    }
    
    public void removeAll() {
        systemNameList.clear();
    }
    
    public void addCollection(List<String> list) {
        systemNameList.addAll(list);
    }
    
    public void add(String str) {
        systemNameList.add(str);
    }
    
    public void remove(int position) {
        systemNameList.remove(position);
    }
    
    @Override
    public int getCount() {
        return systemNameList.size();
    }
    
    @Override
    public Object getItem(int position) {
        return systemNameList.get(position);
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.systemsetup_list_item, parent, false);
            holder = new ViewHolder();
            holder.setup_name = (TextView)convertView.findViewById(R.id.setup_name);
            holder.cbAutoLogin = (CheckBox)convertView.findViewById(R.id.cbzdtb);
        } else {
            holder = (ViewHolder)convertView.getTag();
        }
        aq = new AQuery(convertView);
        if (position == 1) {
            aq.id(R.id.cbzdtb).checked(false).visible();
            aq.id(R.id.layzdtb).clickable(true);
            holder.cbAutoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        MsgServiceImpl msgServiceImpl = new MsgServiceImpl(context);
                        msgServiceImpl.clearIM();
                        msgServiceImpl.clearMsgModel();
                        context.sendBroadcast(new Intent("com.im.delmessage"));
                    }
                }
            });
        } else if (position == 2) {
            aq.id(R.id.cbzdtb).checked(Utils.getSound(context)).visible();
            aq.id(R.id.layzdtb).clickable(true);
            holder.cbAutoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // Utils.SaveDWTXL(context, isChecked);
                    Utils.setSound(context, isChecked);
                    if (isChecked) {
                        Utils.showToast(context, "声音");
                    } else {
                        // Utils.stopMsgService(thisRef);
                        Utils.showToast(context, "震动");
                    }
                }
            });
            // }
            // else if (position == 3) {
            // aq.id(R.id.cbzdtb).checked(Utils.getDWTXL(context)).visible();
            // aq.id(R.id.layzdtb).clickable(true);
            // holder.cbAutoLogin.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            // @Override
            // public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Utils.SaveDWTXL(context, isChecked);
            // if (isChecked) {
            // // Utils.startMsgService(thisRef);
            // Utils.showToast(context, "单位通讯录自动同步已开启");
            // } else {
            // // Utils.stopMsgService(thisRef);
            // Utils.showToast(context, "单位通讯录自动同步已关闭");
            // }
            // }
            // });
        } else {
            aq.id(R.id.cbzdtb).gone();
            aq.id(R.id.layzdtb).clickable(false);
        }
        
        String setupStr = systemNameList.get(position);
        holder.setup_name.setText(setupStr);
        
        convertView.setTag(holder);
        return convertView;
    }
    
    public final class ViewHolder {
        public TextView setup_name;
        
        public CheckBox cbAutoLogin;
    }
}
